package com.weipai.springboot.module.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.weipai.springboot.enums.RetCode;
import com.weipai.springboot.model.Admin;
import com.weipai.springboot.model.CashRecord;
import com.weipai.springboot.model.Customer;
import com.weipai.springboot.model.api.PageResult;
import com.weipai.springboot.model.api.Response;
import com.weipai.springboot.module.service.CashRecordService;
import com.weipai.springboot.module.service.CustomerService;
import com.weipai.springboot.module.service.FileUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Random;

/**
 * @Author: 曹朋飞
 * @Date: 2018/08/09 11:27
 * <p>
 * 客户控制类
 */
@RestController
public class CustomerController {

    @Autowired
    CustomerService customerService;
    @Autowired
    FileUploadService fileUploadService;
    @Autowired
    private CashRecordService cashRecordService;

    /**
     * 用户注册                            管理员
     *
     * @param mobile：手机号
     * @param newCode：输入的验证码
     * @param oldCode：第三方的验证码
     * @param password：密码
     * @param inviCode：邀请码
     * @return
     */
//    @PostMapping("/customer/register")
    @ResponseBody
    @RequestMapping("/customer/register")
    public Response register(String mobile,
                             String newCode,
                             String oldCode,
                             String password,
                             String inviCode,
                             @RequestParam(value = "code", required = false) String code) {
        return customerService.save(mobile, newCode, oldCode, password, inviCode, code);
    }


    /**
     * 分页查询管理员列表
     *
     * @param page      页码
     * @param size      每页条数
     * @param draw      前台参数，照样返回
     * @param search    搜索，如果无搜索此值为paging
     * @param type      客户类型：1会员，2大客户，4员工
     * @param statement
     */
    @RequestMapping(value = "/customer/list", method = RequestMethod.GET)
    public Response list(@RequestParam(value = "page") Integer page,
                         @RequestParam(value = "size") Integer size,
                         @RequestParam(value = "draw") String draw,
                         @RequestParam(value = "search") String search,
                         @RequestParam(value = "type", required = false) String type,
                         @RequestParam(value = "statement", required = false) String statement) {
        return customerService.list(page, size, draw, search, type, statement);
    }

    /**
     * 查询某一员工的推荐客户
     *
     * @param page
     * @param size
     * @param adminCode
     * @param search
     * @return
     */
    @RequestMapping(value = "/customer/listByAdminCode", method = RequestMethod.GET)
    public Response listByAdminCode(@RequestParam(value = "page") Integer page,
                                    @RequestParam(value = "size") Integer size,
                                    @RequestParam(value = "draw") String draw,
                                    @RequestParam(value = "adminCode") String adminCode,
                                    @RequestParam(value = "search", required = false) String search) {
        return customerService.listByAdminCode(page, size, draw, adminCode, search);
    }


    /*
            忘记密码    密码重置
     */
    @ResponseBody
    @RequestMapping("/customer/resetPasswords")
    public Response resetPasswords(@RequestParam(value = "mobile") String mobile,
                                   @RequestParam(value = "newCode") String newCode,
                                   @RequestParam(value = "oldCode") String oldCode,
                                   @RequestParam(value = "password") String password,
                                   @RequestParam(value = "password2") String password2) {
        return customerService.resetPasswords(mobile, newCode, oldCode, password, password2);
    }


    /*
          验证码验证  重置手机号
    */
    @ResponseBody
    @RequestMapping("/customer/resetPhone")
    public Response resetPhone(@RequestParam(value = "customerLoginName") String customerLoginName,
                               @RequestParam(value = "password") String password,
                               @RequestParam(value = "customerId") String customerId,
                               @RequestParam(value = "newCode") String newCode,
                               @RequestParam(value = "oldCode") String oldCode) {
        return customerService.resetPhone(customerLoginName, password, customerId, newCode, oldCode);
    }


    /**
     * 分页查询
     * @param page 页码
     * @param size 每页条数
     * @param draw 前台参数，照样返回
     * @param search 搜索，如果无搜索此值为paging
     * @param sort time，integral，reputation
     * @param way desc，asc，noway
     */
    /*@GetMapping("/customer/list/{page}/{size}/{draw}/{search}/{sort}/{way}")
    public Response list(@PathVariable("page") Integer page,
                                    @PathVariable("size") Integer size,
                                    @PathVariable("draw") String draw,
                                    @PathVariable("search") String search,
                                    @PathVariable("sort") String sort,
                                    @PathVariable("way") String way){
        Response response = new Response();
        PageHelper.startPage(page, size);
        List<Customer> list = customerService.selectAllByDel(0,search, sort, way);
        PageInfo<Customer> pageInfo = new PageInfo<>(list);
        if(list.size()>0){
            response.setCode(RetCode.SUCCESS);
            response.put("pageData", pageInfo.getList());
            response.put("recordsTotal", pageInfo.getTotal());
            response.put("draw", draw);
            response.put("pages", pageInfo.getPages());
        }else{
            response.setCode(RetCode.NO_DATA);
        }
        return response;
    }*/

    /**
     * 查询用户详情
     *
     * @param id:用户的id
     * @return
     */
    @GetMapping("/customer/getCustomerById/{id}")
    public Response getCustomerById(@PathVariable("id") String id) {
        return customerService.getCustomerById(id);
    }

    /**
     * 修改用户资料
     *
     * @param request
     * @param customer
     * @param
     * @return
     */
    @RequestMapping("/customer/update")
    public Response update(HttpServletRequest request, Customer customer, MultipartFile img) {
        Response response = new Response();
        if (img != null) {
            String result = fileUploadService.saveImgUpload(request, img);
            if (result.equals("formatError")) {
                response.setCode(RetCode.IMG_FORMAT);
            } else if (result.equals("sizeover")) {
                response.setCode(RetCode.IMG_SIZE);
            } else if (result.equals("error")) {
                response.setCode(RetCode.IMG_ERROR);
            } else {
                customer.setCustomerHeadImg(result);
                response = customerService.update(customer);
            }
        } else {
            response = customerService.update(customer);
        }
        return response;
    }

    /**
     * 删除
     *
     * @param id:用户的id
     * @return
     */
    @DeleteMapping("/customer/deleteById/{id}")
    public Response deleteById(@PathVariable("id") String id) {
        return customerService.deleteById(id);
    }

    /**
     * 更改用户的状态
     *
     * @param id:用户的id
     * @return
     */
    @GetMapping("/customer/changeStatus/{id}/{status}")
    public Response changeStatus(@PathVariable("id") String id, @PathVariable("status") Integer status) {
        return customerService.changeStatus(id, status);
    }

    /**
     * 批量删除
     *
     * @param id:技工的id集合
     * @return
     */
    @DeleteMapping("/customer/batchDeleteById/{id}")
    public Response batchDeleteById(@PathVariable("id") String[] id) {
        return customerService.batchDeleteById(id);
    }

    /**
     * 更改用户的昵称
     *
     * @param customerId：用户id
     * @param nickname：昵称
     * @return
     */
    @GetMapping("/customer/changeNickname/{customerId}/{nickname}")
    public Response changeNickname(@PathVariable("customerId") String customerId,
                                   @PathVariable("nickname") String nickname) {
        return customerService.changeNickname(customerId, nickname);
    }

    /**
     * 更改会员的等级
     *
     * @param customerId：用户id
     * @param memberLevelId：等级的id
     * @return
     */
    @GetMapping("/customer/changeCustomerMemberLevel/{customerId}/{memberLevelId}/{remark}")
    public Response changeCustomerMemberLevel(@PathVariable("customerId") String customerId,
                                              @PathVariable("memberLevelId") String memberLevelId,
                                              @PathVariable("remark") String remark) {
        return customerService.changeCustomerMemberLevel(customerId, memberLevelId, remark);
    }

    /**
     * 更改会员的类别
     *
     * @param customerId：用户的id
     * @param categoryId：用户类别的id
     * @return
     */
    @GetMapping("/customer/changeCustomerCategory/{customerId}/{categoryId}")
    public Response changeCustomerCategory(@PathVariable("customerId") String customerId,
                                           @PathVariable("categoryId") String categoryId) {
        return customerService.changeCustomerCategory(customerId, categoryId);
    }

    /**
     * 授信-信誉值
     *
     * @param customerId
     * @param reputation
     * @return
     */
    @RequestMapping(value = "/customer/saveCustomerReputation", method = RequestMethod.GET)
    public Response saveCustomerReputation(@RequestParam(value = "customerId") String customerId,
                                           @RequestParam(value = "reputation") Integer reputation) {
        return customerService.saveCustomerReputation(customerId, reputation);
    }


    /**
     * 更改用户的信誉可用值
     *
     * @param customerId：用户id
     * @param useReputation：用户的可用信用值
     * @return
     */
    @RequestMapping(value = "/customer/changeCustomerReputation", method = RequestMethod.GET)
    public Response changeCustomerReputation(@RequestParam(value = "customerId") String customerId,
                                             @RequestParam(value = "useReputation", required = false) Integer useReputation) {
        return customerService.changeCustomerReputation(customerId, useReputation);
    }

    //我的收益
    //根据用户类型判断返回员工收益列表或技工收益列表
    @GetMapping("/customer/getMyIncomes")
    Response getMyIncomes() {
        return customerService.getMyIncomes();
    }

    //我的团队
    @GetMapping("/customer/getMyTeams")
    Response getMyTeams() {
        return customerService.getMyTeams();
    }

    //获取客户银行卡，余额信息
    @RequestMapping("/customer/getBalance")
    Response getBalance(String customerId) {
        return customerService.getBalance(customerId);
    }


    //新增提现记录
    @RequestMapping("/customer/balanceRecord")
    Response getBalance(CashRecord cashRecord) {
        return cashRecordService.saveBalance(cashRecord);
    }

    //更改提现审核状态
    @PostMapping("/order/updateBalance")
    public Response updateBalance(String cashRecordId,String status) {
        return cashRecordService.updateBalance(cashRecordId,status);
    }

    //财务审核提现记录(员工)
    @RequestMapping(value = "/order/staffAudit", method = RequestMethod.GET)
    public Response staffAudit(@RequestParam(value = "status") Integer status,
                                    @RequestParam(value = "page") Integer page,
                                    @RequestParam(value = "size") Integer size,
                                    @RequestParam(value = "draw") String draw,
                                    @RequestParam(value = "targetType") Integer targetType,
                                    @RequestParam(value = "search", required = false) String search,
                                    @RequestParam(value = "startTime", required = false) String startTime,
                                    @RequestParam(value = "endTime", required = false) String endTime){
        return cashRecordService.staffAudit(status, page, size, draw, search, startTime, endTime,targetType);
    }



//    //财务审核提现记录(员工)
//    @RequestMapping(value = "/order/artisanAudit", method = RequestMethod.GET)
//    public Response artisanAudit(@RequestParam(value = "status") Integer status,
//                                    @RequestParam(value = "page") Integer page,
//                                    @RequestParam(value = "size") Integer size,
//                                    @RequestParam(value = "draw") String draw,
//                                    @RequestParam(value = "search", required = false) String search,
//                                    @RequestParam(value = "startTime", required = false) String startTime,
//                                    @RequestParam(value = "endTime", required = false) String endTime){
//        return cashRecordService.artisanAudit(status, page, size, draw, search, startTime, endTime);
//    }
}
